package com.woniu.main.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.woniu.main.goods.pojo.Administrators;
import com.woniu.main.goods.service.AdministratorService;


public class AdministratorsRealm extends AuthorizingRealm {

	@Autowired
	private AdministratorService administratorService;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		return null;
	}

	// 用户认证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// 获取用户提交的账户
		String account = (String) token.getPrincipal();
		Administrators administrators = administratorService.findAdministratorsByAccount(account);
		if (administrators == null) {
			return null;
		} else {
			ByteSource salt = ByteSource.Util.bytes(administrators.getAccount_name());
			SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(administrators.getAccount_name(), administrators.getPassword(), salt,
					getName());
			return info;
		}

	}

}
